//
//  JBUnitTileView.m
//  JBCalendar
//
//  Created by YongbinZhang on 7/8/13.
//  Copyright (c) 2013 JustBen. All rights reserved.
//

#import "JBUnitTileView.h"
#import "JBUnitGridView.h"


@interface JBUnitTileView ()

@property (nonatomic, weak) JBUnitGridView *unitGridView;
@property (nonatomic, assign) UnitTilePosition position;

@property (nonatomic, assign) BOOL selected;

@property (nonatomic, strong) UITapGestureRecognizer *tapGR;
- (void)selectorForTapGR:(UITapGestureRecognizer *)tapGR;
@property (nonatomic, strong) UITapGestureRecognizer *doubleTapGR;
- (void)selectorForDoubleTapGR:(UITapGestureRecognizer *)doubleTapGR;
@property (nonatomic, strong) UILongPressGestureRecognizer *longPressGR;
- (void)selectorForLongPressGR:(UILongPressGestureRecognizer *)longPressGR;

@end

@implementation JBUnitTileView

#pragma mark -
#pragma mark - init
- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        self.tapGR = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(selectorForTapGR:)];
        
        self.doubleTapGR = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(selectorForDoubleTapGR:)];
        [self.doubleTapGR setNumberOfTapsRequired:2];
        
        self.longPressGR = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(selectorForLongPressGR:)];
    }
    return self;
}

/****************************************************************
 *@Description:初始化方法
 *@Params:
 *  frame:用来设置大小
 *  position:位置
 *@Return:SKUnitTileView对象
 ***************************************************************/
- (id)initWithFrame:(CGRect)frame Position:(UnitTilePosition)position
{
    self = [self initWithFrame:frame];
    if (self) {
        self.position = position;
        
        self.backgroundView = [[UIView alloc] initWithFrame:self.bounds];
        self.backgroundView.backgroundColor = [UIColor clearColor];
        [self addSubview:self.backgroundView];
        
        self.highlightedBackgroundView = [[UIView alloc] initWithFrame:self.bounds];
        self.highlightedBackgroundView.backgroundColor = [UIColor clearColor];
        self.highlightedBackgroundView.hidden = YES;
        [self addSubview:self.highlightedBackgroundView];
        
        self.dayLabel = [[UILabel alloc] initWithFrame:self.bounds];
        self.dayLabel.textColor = [UIColor blackColor];
#if __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_6_0
        self.dayLabel.textAlignment = UITextAlignmentCenter;
#else
        self.dayLabel.textAlignment = NSTextAlignmentCenter;
#endif
        [self addSubview:self.dayLabel];
    }
    
    return self;
}


#pragma mark -
#pragma mark - Class Extensions
- (void)selectorForTapGR:(UITapGestureRecognizer *)tapGR
{
    NSAssert(self.unitGridView, @"Any JBUnitTileView object must be contained in a JBUnitGridView object");
    
    [self tappedOnUnitTileWithAnimated:self.animated];
    
    if ([self.unitGridView.delegate respondsToSelector:@selector(unitGridView:tappedOnUnitTileView:)]) {
        [self.unitGridView.delegate unitGridView:self.unitGridView tappedOnUnitTileView:self];
    }
}

- (void)selectorForDoubleTapGR:(UITapGestureRecognizer *)doubleTapGR
{
    NSAssert(self.unitGridView, @"Any JBUnitTileView object must be contained in a JBUnitGridView object");

    if ([self.unitGridView.delegate respondsToSelector:@selector(unitGridView:doubleTappedOnUnitTileView:)]) {
        [self.unitGridView.delegate unitGridView:self.unitGridView doubleTappedOnUnitTileView:self];
    }
}

- (void)selectorForLongPressGR:(UILongPressGestureRecognizer *)longPressGR
{
    NSAssert(self.unitGridView, @"Any JBUnitTileView object must be contained in a JBUnitGridView object");

    if ([self.unitGridView.delegate respondsToSelector:@selector(unitGridView:longPressedOnUnitTileView:)]) {
        [self.unitGridView.delegate unitGridView:self.unitGridView longPressedOnUnitTileView:self];
    }
}

#pragma mark -
#pragma mark - Object Methods
/****************************************************************
 *@Description:该Tile激活状态发生变化（模版方法）
 *@Params:
 *  highlighted:YES-被激活   NO-失去激活
 *  animated:YES-延时 NO-非延时
 *@Return:nil
 ***************************************************************/
- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated
{
    if (highlighted) {
        if (animated) {
            self.highlightedBackgroundView.hidden = NO;
            self.backgroundView.hidden = YES;
        } else {
            self.highlightedBackgroundView.hidden = NO;
            self.backgroundView.hidden = YES;
        }
    } else {
        if (animated) {
            self.highlightedBackgroundView.hidden = YES;
            self.backgroundView.hidden = NO;
        } else {
            self.highlightedBackgroundView.hidden = YES;
            self.backgroundView.hidden = NO;
        }
    }
}

/****************************************************************
 *@Description:用户按下该Tile（模版方法）
 *@Params:
 *  animated:YES-切换效果延时 NO-切换效果非延时
 *@Return:nil
 ***************************************************************/
- (void)tappedOnUnitTileWithAnimated:(BOOL)animated
{
    [self setHighlighted:YES animated:animated];
    
    //  TO DO
}

/****************************************************************
 *@Description:用户双击该Tile（模版方法）
 *@Params:
 *  animated:YES-切换效果延时 NO-切换效果非延时
 *@Return:nil
 ***************************************************************/
- (void)doubleTappedOnUnitTileWithAnimated:(BOOL)animated
{
    [self setHighlighted:YES animated:animated];
    
    //  TO DO
}

/****************************************************************
 *@Description:用户长时间按下该Tile（模版方法）
 *@Params:
 *  animated:YES-切换效果延时 NO-切换效果非延时
 *@Return:nil
 ***************************************************************/
- (void)longPressedOnUnitTileWithAnimated:(BOOL)animated
{
    [self setHighlighted:YES animated:animated];
    
    //  TO DO
}


#pragma mark -
#pragma mark - Settors
- (void)setPosition:(UnitTilePosition)position
{
    if (position.line != _position.line || position.row != _position.row) {
        _position = position;
        self.frame = CGRectMake(self.bounds.size.width * self.position.line, self.bounds.size.height * self.position.row, self.bounds.size.width, self.bounds.size.height);
    }
}

- (void)setDate:(JBCalendarDate *)date
{
    if (![date isEqual:_date]) {
        _date = date;
        
        if (self.dayLabel) {
            self.dayLabel.text = [NSString stringWithFormat:@"%i", _date.day];
        }
    }
}

- (void)setSelected:(BOOL)selected
{
    if (selected != _selected) {
        _selected = selected;
    }
}

@end
